//
//  NewMissionListCell.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2018/4/2.
//  Copyright © 2018年 zjhcsoftios. All rights reserved.
//

#import "NewMissionListCell.h"
#import <Masonry/Masonry.h>
#import "UIImageView+WebCache.h"
#import "UIView+BlocksKit.h"
#import "UIColor+Ex.h"
#import <MJExtension/MJExtension.h>
#import "NetworkAddress.h"

#define iSEESCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

@interface NewMissionListCell ()

@property (nonatomic, strong) UIView *buttomView;

@property (nonatomic, strong) UILabel *statusLabel;

@property (nonatomic, strong) UILabel *rewardLabel;

@property (nonatomic, strong) UIButton *selectButton;

@end

@implementation NewMissionListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setButtomLine];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initStatusLabelAndRewardLabelAndSelectButton];
    }
    return self;
}

- (void)initStatusLabelAndRewardLabelAndSelectButton {
    
    _statusLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_statusLabel];
    [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
        //            make.bottom.mas_equalTo(self.focusImageView.mas_top).offset(-3);
        make.centerY.mas_equalTo(self.contentView);
    }];
    _statusLabel.textColor = [UIColor colorWithHexString:@"3BBF8E"];
    _statusLabel.font = [UIFont systemFontOfSize:12];
    
    _rewardLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_rewardLabel];
    [_rewardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
        make.top.mas_equalTo(self.statusLabel.mas_bottom).offset(4);
    }];
    _rewardLabel.font = [UIFont systemFontOfSize:14];
    
    _selectButton = [[UIButton alloc] init];
    [_selectButton setImage:[self getImageFromBundle:@"uncheck"] forState:UIControlStateNormal];
    [_selectButton setImage:[self getImageFromBundle:@"checked"] forState:UIControlStateSelected];
    [_selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectButton];
    [_selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(22);
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView.mas_left).offset(14);
    }];
    
    UIView *tapView = [[UIView alloc] init];
    [self addSubview:tapView];
    [tapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(self.contentView);
        make.width.mas_equalTo(45);
    }];
    [tapView bk_whenTapped:^{
        [self selectButtonAction:self.selectButton];
    }];
    
}


- (void)setDataList:(NSArray *)dataList {
    NSMutableArray *list = [NSMutableArray array];
    for (NSDictionary *dic in dataList) {
        if ([dic[@"t"] integerValue] == 1) {
            [list addObject:dic];
        }
    }
    _dataList = [NSArray arrayWithArray:list];
    
    [self setLabels];
}

- (void)setModel:(NewMissionListDetailModel *)model {
    _model = model;
    self.dataList = [NSDictionary mj_objectArrayWithKeyValuesArray:model.content];
    self.statusLabel.text = model.status;
    self.rewardLabel.text = model.commission;
    if (_model.isSelect) {
        self.selectButton.selected = _model.isSelect;
    }else {
        _model.isSelect = NO;
        self.selectButton.selected = _model.isSelect;
    }
    [self setImages];
}





- (void)setIsLastCell:(BOOL)isLastCell {
    _isLastCell = isLastCell;

    self.buttomView.hidden = _isLastCell;
}


- (void)setButtomLine {
    _buttomView = [[UIView alloc] init];
    [self addSubview:_buttomView];
    [_buttomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self);
        make.height.mas_equalTo(@1);
    }];
    _buttomView.backgroundColor = [UIColor colorWithHexString:@"EAECEF"];
    
}

- (void)setLabels {
    
    UILabel *prevLabel = nil;
    
    for (int i = 0; i < _dataList.count; i++) {
        NSDictionary *dic = _dataList[i];
        UILabel *label = [self viewWithTag:100 + i];
        if (!label) {
            
            label = [[UILabel alloc] init];
            label.font = [UIFont systemFontOfSize:14];
            label.numberOfLines = 0;
            label.tag  = 100 + i;
            
            [self.contentView addSubview:label];
        }
        
        NSString *title = [NSString stringWithFormat:@"%@: ",dic[@"k"]];
        NSString *content = dic[@"v"];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",title,content]];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"999999"] range:NSMakeRange(0, title.length - 1)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"444444"] range:NSMakeRange(title.length,  str.length - title.length)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, str.length)];
        label.attributedText = str;
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).with.offset(45);
            make.right.equalTo(self.contentView.mas_right).with.offset(-115);
            if (prevLabel) {
                make.top.equalTo(prevLabel.mas_bottom).with.offset(3);
            }else {
                make.top.equalTo(self.contentView.mas_top).with.offset(15);
            }
            if (_dataList.count - 1 == i) {
                make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-15);
            }
        }];
        
        prevLabel = label;
    }
}

- (void)setImages {
    
    
    for (int i = 0; i < _model.iconList.count; i++) {
        UIImageView *tempImage = [self viewWithTag:200 + i];
        if (!tempImage) {
            tempImage = [[UIImageView alloc] init];
            [self.contentView addSubview:tempImage];
            [tempImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.mas_equalTo(16);
                make.right.equalTo(self.contentView.mas_right).with.offset(-15 - (21 * i));
                make.bottom.equalTo(self.statusLabel.mas_top).with.offset(-3);
            }];
            tempImage.tag = 200 + i;
        }
 
        
        [tempImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", NMarketMainUrl, _model.iconList[i]]]];

    }
    
    for (UIView *view in self.contentView.subviews) {
        if (view.tag - 200 >= _model.iconList.count && [view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
}

#pragma mark 按钮点击

- (void)selectButtonAction:(UIButton *) buttom {
    
    self.model.isSelect = !self.model.isSelect;
    buttom.selected = self.model.isSelect;
    
    if ([_delegate respondsToSelector:@selector(NewMissionListCellSelectButtonAction:)]) {
        [_delegate NewMissionListCellSelectButtonAction:self.model];
    }
}


#pragma mark 懒加载
//- (UIImageView *)focusImageView {
//    if (!_focusImageView) {
//        _focusImageView = [[UIImageView alloc] init];
//        [self.contentView addSubview:_focusImageView];
//        [_focusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(self.contentView.mas_right).with.offset(-15);
//            make.width.height.mas_equalTo(16);
//            make.centerY.mas_equalTo(self.contentView);
//        }];
//        _focusImageView.image = [UIImage imageNamed:@"zhong"];
//    }
//    return _focusImageView;
//}
//
//- (UIImageView *)startImageView {
//    if (!_startImageView) {
//        _startImageView = [[UIImageView alloc] init];
//        [self.contentView addSubview:_startImageView];
//        [_startImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(self.focusImageView.mas_left).with.offset(-5);
//            make.width.height.mas_equalTo(16);
//            make.centerY.mas_equalTo(self.contentView);
//        }];
//        _startImageView.image = [UIImage imageNamed:@"xing"];
//    }
//    return _startImageView;
//}
//
//- (UIImageView *)urgeImageView {
//    if (!_urgeImageView) {
//        _urgeImageView = [[UIImageView alloc] init];
//        [self.contentView addSubview:_urgeImageView];
//        [_urgeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(self.startImageView.mas_left).with.offset(-5);
//            make.width.height.mas_equalTo(16);
//            make.centerY.mas_equalTo(self.contentView);
//        }];
//        _urgeImageView.image = [UIImage imageNamed:@"cui"];
//    }
//    return _urgeImageView;
//}


- (UIImage *)getImageFromBundle:(NSString *)imageName {
    return [UIImage imageNamed:imageName inBundle:[NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"NewMarketLib" withExtension:@"bundle"]] compatibleWithTraitCollection:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
